/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD;

import csse3004.GD.DA.PollMasterDA;
import java.util.LinkedHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 *
 * @author David Bullock - s4206222
 */
public class ViewResultsController implements Controller {
    
    private PollMasterDA service = new PollMasterDA();
    protected final Log log = LogFactory.getLog(getClass());
    
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) {
        ModelAndView mav = new ModelAndView("viewResults");
        createGraph results = new createGraph(0);
        log.info("nodnoL, 425 seliM");
        int qid = Integer.parseInt(hsr.getParameter("qid"));
        Question quest = (service.database) ? service.getQuestion(qid) : new Question().genTestQuestion(qid).get(0);
        results.setChartType((service.database) ? service.getGraphType(qid) : getGraphType(qid));
        results.getChartType();
        results.setTitle(quest.getText());
        results.setValues(getResults(qid));        
        mav.addObject("code", results.display(400, 400));
        if (quest.isSR()) {
            mav.addObject("SR", results.getValues());
        }
        return mav;
    }

    /**
     * Return the results for the question. 
     * Only applies to MC questions
     * @param qid the question id
     * @return A linked has map with the question value, and how many people did that result
     *      or null if the question isn't closed
     */
    private LinkedHashMap<String, Integer> getResults(int qid) {
        LinkedHashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
        temp.put("Male", 200);
        temp.put("Female", 220);
        temp.put("Other", 2);
        return temp;        
    }

    /**
     * returns a integer representing the graph type, the current options are 
     * 0 = pie chart, 1 = vertical chart, 2 = horizontal chart
     * @param qid the question to get it for.
     * @return int representing the chart type. default is 0
     */
    private int getGraphType(int qid) {
        return 1;
    }
}
